//
//  CateViewController.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/5.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class CateViewController: BaseViewController {

    fileprivate var searchString = ""
    fileprivate var topList = [CateTopLisModel]()
    fileprivate var rankList = [CateRankingListModel]()
    fileprivate lazy var searchButton: UIButton = {
       let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 20, height: 30)
        button.backgroundColor = UIColor.black.withAlphaComponent(0.1)
        button.layer.cornerRadius = 15
        button.setTitleColor(.white, for: .normal)
        button.setImage(UIImage(named: "nav_search")?.withRenderingMode(.alwaysOriginal), for: .normal)
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0)
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5)
        button.addTarget(self, action:#selector(searchAction), for: .touchUpInside)
        return button
    }()
    
    fileprivate lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.alwaysBounceVertical = true
        collectionView.register(CateTopCell.self, forCellWithReuseIdentifier: "CateTopCell")
        collectionView.register(CateRankCell.self, forCellWithReuseIdentifier: "CateRankCell")
        collectionView.refrshHead = RefreshGifHeader{
            self.loadData()
        }
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.titleView = self.searchButton
        
        self.view.addSubview(self.collectionView)
        setUpSnap()
        loadData()
        // Do any additional setup after loading the view.
    }

    fileprivate func loadData() {
        NetworkTool.loadCateData {[weak self] (model) in
            for top in model.topList {
                self?.topList.append(top)
            }
            for rank in model.rankingList {
                self?.rankList.append(rank)
            }
            self?.searchButton.setTitle(model.recommendSearch ?? "", for: .normal)
            self?.collectionView.reloadData()
            self?.collectionView.refrshHead.endRefreshing()
        }
    }

    @objc fileprivate func searchAction() {
        
    }

    fileprivate func setUpSnap() {
        self.collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }

}

extension CateViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return topList.count
        }else {
            return rankList.count
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(10, 10, section == 0 ? 0 : 10, 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = floor(Double(kSCREEN_WIDTH - 40.0) / 3.0)
        return CGSize(width: width, height: (indexPath.section == 0 ? 55 : (width * 0.75 + 30)))
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if  indexPath.section == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CateTopCell", for: indexPath) as! CateTopCell
            cell.model = topList[indexPath.row]
            return cell
        }else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CateRankCell", for: indexPath) as! CateRankCell
            cell.model = rankList[indexPath.row]
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.collectionView.deselectItem(at: indexPath, animated: true)
        if indexPath.section == 0 {
            let model = topList[indexPath.row]
            var titles:[String] = []
            var vcs: [UIViewController] = []
            for tab in model.extra.tabList {
                guard let tabTitle = tab.tabTitle else {
                    continue
                }
                titles.append(tabTitle)
                vcs.append(CateComicsListViewController(argCon: tab.argCon, argName: tab.argName ?? "", argValue: tab.argValue))
            }
            let vc = CateTypeListViewController(titleArr: titles, vcs: vcs)
            vc.title = model.sortName
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        }else {
            let model = rankList[indexPath.row]
            let vc = HomeComicsListViewController(argCon: model.argCon, argName: model.argName ?? "", argValue: model.argValue)
            vc.title = model.sortName
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
}






